#pragma once

#include "IXTimer.h"
#include "XBaseObjImpl.h"

#include <map>

class CXTimerMgrImpl : public CXBaseObjImpl<IXTimerMgr>
{
    typedef CXBaseObjImpl<IXTimerMgr> superclass;
    DEF_SKIN_CLASS_SINGLE_INTF(CXTimerMgrImpl,
        superclass,
        gs_strGuidXSkin,
        IXTimerMgr,
        eIntf_Singleton);

public:
    CXTimerMgrImpl();
    virtual ~CXTimerMgrImpl();

public:
    // IXTimerMgr
    virtual UINT_PTR STDMETHODCALLTYPE  SetTimer(IXTimer* pTimer, UINT uElapse, void* pData);
    virtual UINT_PTR STDMETHODCALLTYPE  SetAsyncTimer(IXTimer* pTimer, UINT uElapse, void* pData);

    virtual UINT_PTR STDMETHODCALLTYPE  ResetTimer(UINT_PTR uId, UINT uElapse);
    virtual void*    STDMETHODCALLTYPE  ResetTimer(UINT_PTR uId, void* pData);
    virtual IXTimer* STDMETHODCALLTYPE  ResetTimer(UINT_PTR uId, IXTimer* pTimer);

    virtual BOOL     STDMETHODCALLTYPE  KillTimer(UINT_PTR uId);
    virtual BOOL     STDMETHODCALLTYPE  KillTimer(IXTimer* pTimer);

public:
    // IXBaseObj
    virtual void STDMETHODCALLTYPE   OnConstruct();
    virtual void    STDMETHODCALLTYPE   OnFinalRelease();

protected:
    // CXTimerMgrImpl
    void        ReleaseData();
    HWND        GetTimerWnd();
    BOOL        HandleTimerMsg(UINT_PTR idEvent);

    static LRESULT CALLBACK TimerWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

protected:
    typedef struct
    {
        void*   pData;
        IXTimer* pTimer;
    } TimerData;
    typedef std::map<UINT, TimerData>  TimerMap;

    UINT_PTR        m_uBaseTimerId;
    TimerMap        m_TimerMap;
    HWND            m_hTimerWnd;
};

